//
//  NSAttributedString+BPCrash.m
//  BPCrashDemo
//
//  Created by SiYugui on 2019/12/30.
//  Copyright © 2019年 SiYugui. All rights reserved.
//

#import "NSAttributedString+BPCrash.h"
#import "BPCrashLog.h"
#import "NSObject+BPSwizzle.h"

@implementation NSAttributedString (BPCrash)
+(void)avoidAttributedStringCrashExchangeMethod{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class NSConcreteAttributedString = NSClassFromString(@"NSConcreteAttributedString");
        [self exchangeInstanceMethod:NSConcreteAttributedString method1:@selector(initWithString:) method2:@selector(__initWithString:)];
        
        [self exchangeInstanceMethod:NSConcreteAttributedString method1:@selector(initWithAttributedString:) method2:@selector(__initWithAttributedString:)];
        
        [self exchangeInstanceMethod:NSConcreteAttributedString method1:@selector(initWithString:attributes:) method2:@selector(__initWithString:attributes:)];
    });
}

-(instancetype)__initWithString:(NSString *)str{
    id object = nil;
    @try {
        object = [self __initWithString:str];
    } @catch (NSException *exception) {
        [BPCrashLog noteErrorWithException:exception attachedToDo:BPCrashDefaultReturnNil];
    } @finally {
        return object;
    }
}

-(instancetype)__initWithAttributedString:(NSAttributedString *)attrStr{
    id object = nil;
    @try {
        object = [self __initWithAttributedString:attrStr];
    } @catch (NSException *exception) {
        [BPCrashLog noteErrorWithException:exception attachedToDo:BPCrashDefaultReturnNil];
    } @finally {
        return object;
    }
}

-(instancetype)__initWithString:(NSString *)str attributes:(NSDictionary<NSAttributedStringKey,id> *)attrs{
    id object = nil;
    @try {
        object = [self __initWithString:str attributes:attrs];
    } @catch (NSException *exception) {
        [BPCrashLog noteErrorWithException:exception attachedToDo:BPCrashDefaultReturnNil];
    } @finally {
        return object;
    }
}
@end
